new Promise((resolve,reject)=>{

})
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'

class myPromise{
    #state = PENDING
    #result = null
    #eventList = []
    constructor(executor){
        function resolve(result){
           this.#changeState(FULFILLED,result)
        }
        function reject(reason){
            this.#changeState(REJECTED,reason)
        }
        try{
            executor(resolve,reject)
        }catch(err){
            reject(err)
        }
        
    }

    #changeState(state,result){
        if(this.#state !==PENDING) return
        this.#state = state;
        this.#result = result
    }

    then(onResolve,onReject){
        return new myPromise((resolve,reject)=>{
            if(this.state === FULFILLED){
                onResolve(this.#result)
            }
            if(this.state === REJECTED){
                onReject(this.#result)
            }
            if(this.state === PENDING){
                this.#eventList.push({
                    onResolve,
                    onReject,
                    resolve, // 决定then返回状态
                    reject
                })
            }
        })
        
        
    }

    #run(){
        if(this.#state !==PENDING) return
        while(this.#eventList.length>0){
            const {onResolve,onReject ,resolve,reject} = this.#eventList.shift()
            
        }
    }

}